package com.choosefine.it.basedata.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.BaseService;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.basedata.dao.CompanyMgtMapper;
import com.choosefine.it.basedata.model.CompanyInfo;
import com.choosefine.it.basedata.model.CompanyMgt;
import com.choosefine.it.basedata.model.User;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.github.pagehelper.PageHelper;

@Service(value = "companyMgtService")
public class CompanyMgtService extends BaseService{
	Logger logger = LoggerFactory.getLogger(CompanyMgtService.class);
	@Autowired
	CustomRestTemplate restTemplate;
	@Autowired
	public CompanyMgtMapper companyMgtMapper;
	@Autowired
	public CompanyInfoService companyInfoService;
	@Autowired
	public UserService userService;

	@Value("#{yaml.get('basedata.companyinfo.getCompanyByUserCode')}")
	 String getCompanyByCompanyCodeURL = null;

	public List<CompanyMgt> selectCompanyMgtList(PageDTO pageDTO) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		return companyMgtMapper.selectCompanyMgtList();
	}

	public List<CompanyMgt> selectCompanyMgtBy(Map<String, Object> map, PageDTO pageDTO) {
		PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
		return companyMgtMapper.selectCompanyMgtBy(map);
	}

	public Integer updateCompanyMgt(String userCode, Integer useStatus, String operator) {
		Map<String, Object> map = new HashMap<>();
		map.put("userCode", userCode);
		map.put("operator", operator);
		map.put("useStatus", useStatus);
		return companyMgtMapper.updateCompanyMgt(map);
	}

	public Integer addCompanyMgt(CompanyInfo companyinfo,Integer useStatus, String operator) {
		Map<String, Object> map = new HashMap<>();
		User user = new User();
		user.setIsValid(companyinfo.getIsValid());
		user.setUserCode(companyinfo.getUserCode());
		user.setUserName(companyinfo.getCompanyName());
		user.setUserType(0);
		user.setCtime(companyinfo.getCtime());
		userService.insert(user);
		map.put("userCode", companyinfo.getUserCode());
		map.put("operator", operator);
		map.put("useStatus", useStatus);
		companyMgtMapper.addCompanyMgt(map);
		return companyInfoService.insert(companyinfo);
	}

	public int deleteCompanyMgt(String userCodes) {
//		String url = deleteCompanyinfoURL;
		// logger.info("deleteCompanyinfoURL="+deleteCompanyinfoURL);
//		restTemplate.delete(url, userCode);
		return companyInfoService.deleteByUserCode(userCodes);
	}

	public CompanyInfo selectCompanyMgtByUserCode(String userCode) {
		String url = URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  getCompanyByCompanyCodeURL).append(userCode).toString();
		CompanyInfo com = new CompanyInfo();
		@SuppressWarnings("unchecked")
		Map<String, String> map = restTemplate.getForObject(url, Map.class);
		logger.info("getUrl:-------------" + url);
		logger.info("result  getForObject:-----------------" + restTemplate.getForObject(url, Map.class));
		logger.info("result:  getForEntity-----------------" + restTemplate.getForEntity(url, CompanyInfo.class));
		com.setBusinessLicense(map.get("businessLicense"));
		com.setBusinessPeriod(map.get("ctime"));
		map.get("isValid");
		map.get("userCode");
		map.get("companyName");
		map.get("companyLegal");
		map.get("enterpriseType");
		map.get("businessScope");
		map.get("companyPicurl");
		map.get("companyRegtime");
		map.get("companyRegmoney");
		map.get("companyAddress");
		map.get("companyScale");
		map.get("contactAuth");
		map.get("contactAddress");
		map.get("contactPhoneNumber");
		map.get("contactUndertaking");
		map.get("companyType");
		map.get("companyIntroduce");
		map.get("license");
		map.get("contactIdcard");
		map.get("contactIdcardValidity");
		map.get("companyProfile");
		map.get("registrationNum");
		map.get("organizationCode");
		map.get("documentType");
		map.get("organizationCodeCertificate");
		map.get("contactIdcardZurl");
		map.get("contactIdcardFurl");
		map.get("businessPeriod");
		map.get("businessTaxNum");
		return com;
	}
}
